#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace UTable.Objects.Controls
{
    internal class UContentControlImpl: UControlImpl
    {
        private ContentControl _owner;
        private IControl _child = null;
        private IControl _container = null;

        void _owner_Loaded(object sender, RoutedEventArgs e)
        {
            // OnContentChanged(null, null);
        }

        internal void OnContentChanged(object oldValue, object newValue)
        {
            if (_owner.IsLoaded)
            {
                // firstly find the container of the content
                IControl container = null;
                ContentPresenter presenter = FindPresenter(_owner);
                if (presenter == null)
                {
                    container = this;
                }
                else
                {
                    container = GetParentControl(presenter);
                }

                if (_child != null && !(_container is UContentPresenter))
                {
                    _container.Info.Children.Remove(_child.Info);
                    _child.Info.Parent = null;
                }
                _container = container;
                if (_owner.Content is IControl && !(_container is UContentPresenter))
                {
                    _container.Info.Children.Add((this._owner.Content as IControl).Info);
                    (this._owner.Content as IControl).Info.Parent = _container.Info;
                    _child = _owner.Content as IControl;
                }
            }
        }

        private IControl GetParentControl(DependencyObject obj)
        {
            while (obj != null)
            {
                if (obj is IControl)
                    return (obj as IControl);
                obj = VisualTreeHelper.GetParent(obj);
            }
            return null;
        }

        private ContentPresenter FindPresenter(DependencyObject obj)
        {
            for (int i=0; i<VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child is ContentPresenter)
                {
                    return child as ContentPresenter;
                }
                if (!(child is ContentControl))
                {
                    ContentPresenter result = FindPresenter(child);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }

        internal static void OverrideMetadata(Type type)
        {
            UControlImpl.OverrideMetadata(type);
            // OverrideMetadata(ContentControl.ContentProperty, type);
        }

        internal UContentControlImpl(ContentControl owner): base(owner)
        {
            _owner = owner;
            _owner.Loaded += new RoutedEventHandler(_owner_Loaded);
        }
    }
}